package test;

public class test {
	private Integer counter;
	private final static int MAX = 1000;

	public test() {
		counter = 0;
	}

	public class CounterThread extends Thread {
		public void run() {
			counter = counter + 1;
		}
	}

	public class SynCounterThread extends Thread {
		private test t;

		public SynCounterThread(test t) {
			this.t = t;
		}

		public/* synchronized */void run() {
			synchronized (t) {
				counter++;
			}
		}
	}

	public static void main(String[] args) {

		test t2 = new test();
		for (int i = 0; i < test.MAX; i++) {
			SynCounterThread th = t2.new SynCounterThread(t2);
			try {
				th.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			th.start();
		}
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Active threads = " + Thread.activeCount());
		System.out.println("SynCounter = " + t2.counter + "\nMax  = " + MAX);

		test t1 = new test();
		for (int i = 0; i < test.MAX; i++) {
			CounterThread th = t1.new CounterThread();
			try {
				th.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			th.start();
		}
		try {
			Thread.sleep(10); // join doesn't work ?
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Active threads = " + Thread.activeCount());
		System.out.println("Counter = " + t1.counter + "\nMax  = " + MAX);

	}

}
